// mircoApp/cloud_shop/cloud_shop_reward_center/cloud_shop_reward_center.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 1,
    page: 1,
    total_page: 0,
    isloadMore: true,
    dataList: [],
    nowTime: "",
    running_activity_count: 0,
    expect_activity_count: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      nowTime: Date.parse(new Date()) / 1000,
    })
    this.init();
    this.getData();
  },
  init() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      dataList: []
    })
  },
  tabChange(e) {
    this.setData({
      active: e.detail.name
    })
    console.log(this.data.active)
    this.init();
    this.getData();
  },
  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.activity.activityList"),
      data: {
        time_type: this.data.active
      }
    })
    if (data.result) {
      this.setData({
        dataList: data.data.data,
        total_page: data.data.last_page,
        isloadMore: true,
        running_activity_count: data.data.running_activity_count,
        expect_activity_count: data.data.expect_activity_count
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let json = {
        page: this.data.page,
        time_type: this.data.active
      }
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.cloud-shop.frontend.activity.activityList"),
        data: json
      })
      this.data.isloadMore = true;
      if (data.result) {
        this.setData({
          dataList: this.data.dataList.concat(data.data.data)
        })
      } else {
        app.tips(data.msg)
      }
    }
  },
  toDetail(e) {
    let {item} = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/mircoApp/cloud_shop/full_Return_Details/full_Return_Details?id=${item.id}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.isLoadMore){
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})